<%
# Copyright (C) 2012 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  css_bundle :profile_show, :tinymce
  js_bundle :profile_show
  @body_classes << 'not-editing'
  js_env PROFILE: @user_data if @user_data[:can_edit]
  js_env(folder_id: @user.profile_pics_folder.id) if @user == @current_user
%>
<%= form_tag update_profile_profile_path, { :method => :put, :id => :edit_profile_form, :class => "ic-Form-control" } do %>
  <div class="image-block">
    <div class="image-block-image profile-avatar-wrapper">
      <% if @user_data[:can_edit] %>
        <%= avatar(@user_data[:id], size: 128, url: '#', edit: true, class: "profile-link", title: t('titles.click_to_change_profile_pic', "Click to change profile pic")) %>
      <% elsif @user != @current_user %>
        <%= avatar(@user, size: 128, url: report_avatar_image_url(@user.id),
          class: "report_avatar_link", title: t("Click to report profile picture"), show_flag: true) %>
      <% else %>
        <%= avatar(@user_data[:id], size: 128, url: nil) %>
      <% end %>
    </div>

    <div class="profileContent__Block">
      <% if @user_data[:can_edit_name] %>
        <h2 class="hide-if-editing"><%= @user_data[:short_name] %></h2>
        <label class="show-if-editing ic-Label">{{#t}}Name{{/t}}:
          <input aria-label="<%= t 'Name' %>" id="name_input" class="ic-Input" type="text" name="user[short_name]" value="<%= @user_data[:short_name] %>" required>
        </label>
        <p class="profileDetails">
          <span class="hide-if-editing"><%= @user_data[:title] %></span>
          <label class="show-if-editing ic-Label">{{#t}}Title{{/t}}:
            <input aria-label="<%= t 'Title' %>" class="ic-Input" type="text" name="user_profile[title]" value="<%= @user_data[:title] %>">
          </label>
        </p>
      <% else %>
        <h2><%= @user_data[:short_name] %></h2>
        <p><%= @user_data[:title] %></p>
      <% end %>

      <div class="show-if-editing edit-contact-methods">
        <h3 class="profileHeader">{{#t}}Contact{{/t}}</h3>
          <% if @user_data[:user_services].present? %>
            <ul class="social-links">
              <li>
                <label for="social_message" class="social hide-text message ic-Label" title="<%= t :message_title, 'Message' %>" data-tooltip><%= t :message_me, 'message me in Canvas' %></label>
              </li>
              <% @user_data[:user_services].each do |s| %>
                <li>
                  <% service_name = s[:service_name] || s[:service].to_s.titleize %>
                  <label for="social_<%= s[:service] %>" class="social hide-text <%= s[:service] %> ic-Label" title="<%= service_name %>" data-tooltip><%= t :service_contact, 'contact %{user} with %{service}', :user => @user_data[:short_name], :service => service_name %></label>
                  <input class="ic-Input" type="hidden" value="0" name="user_services[<%= s[:service] %>">
                  <%= check_box_tag "user_services[#{s[:service]}]", 1, s[:visible], :id => "social_#{s[:service]}" %>
                </li>
              <% end %>
            </ul>
            <div class="social-instructions">
              <p>
                {{#t}}Check the contact methods you'd like to be visible to others on your profile.{{/t}}<br>
                <%= link_to(t(:manage_settings, "Manage Registered Services"), settings_profile_path)  %>
              </p>
            </div>
          <% else %>
            <p><%= t('#profile.you_have_no_services', "No registered services, you can add some on the *settings* page.", :name => @user_data[:short_name], :wrapper => link_to('\1', settings_profile_path)) %></p>
          <% end %>
      </div>

      <div class="hide-if-editing">
        <h3 class="profileHeader"><%= t :ways_to_contact_user, "Contact" %></h3>
        <% if @user_data[:user_services].blank? && @user == @current_user %>
          <p><%= t('#profile.you_have_no_services', "No registered services, you can add some on the *settings* page.", :name => @user_data[:short_name], :wrapper => link_to('\1', settings_profile_path)) %></p>
        <% else %>
          <ul class="social-links">
            <% if current_user_is_account_admin && @user != @current_user %>
              <li><a href="<%= message_user_path(@user) %>" class="social hide-text message" title="<%= t :message_title, 'Message' %>" data-tooltip><%= t :message_user_canvas, 'Message %{user} in Canvas', :user => @user_data[:short_name] %></a></li>
            <% end %>
            <% if @user_data[:user_services].present? && @user.show_user_services %>
              <% @user_data[:user_services].select{|s| s[:visible] }.each do |s| %>
                <% service_name = s[:service_name] || s[:service].to_s.titleize %>
                <li><a href="<%= s[:service_user_link] %>" class="social hide-text <%= s[:service] %>" title="<%= service_name %>" data-tooltip><%= t :service_contact, 'contact %{user} with %{service}', :user => @user_data[:short_name], :service => service_name %></a></li>
              <% end %>
            <% end %>
          </ul>
        <% end %>
      </div>

      <div>
        <h3 id="profile_bio_label" class="profileHeader"><%= t :bio, "Biography" %></h3>
        <% if @user_data[:bio] %>
          <div class="hide-if-editing"><%= raw format_message(@user_data[:bio]).first %></div>
        <% else %>
          <p class="hide-if-editing"><%= t('user_has_no_bio', "%{user} hasn't added a bio", :user => @user_data[:short_name]) %></p>
        <% end %>
        <% if @user_data[:can_edit] %>
          <div class="show-if-editing">
            <textarea class="ic_Input" id="profile_bio" name="user_profile[bio]" aria-labelledby="profile_bio_label"><%= @user_data[:bio] %></textarea>
          </div>
        <% end %>
      </div>

      <div>
        <% if @user != @current_user %>
          <div class="hide-if-editing">
            <h3 class="profileHeader"><%= t :user_enrolled, "Enrollments" %></h3>
            <% if @user_data[:common_contexts] %>
              <ul>
                <% @user_data[:common_contexts].each do |context| %>
                  <li class="profileEnrollment__Items"><%= context[:roles].join(', ') %> in <a href="<%= context[:html_url] %>"><%= context[:name] %></a></li>
                <% end %>
              </ul>
            <% else %>
              <p>{{#t}}You don't have anything in common{{/t}}</p>
            <% end %>
          </div>
        <% end %>
      </div>

      <div class="profile_links">
        <h3 class="profileHeader"><%= t :links, "Links" %></h3>
        <div class="hide-if-editing">
          <% if @user_data[:links].length > 0 %>
            <ul class="profileLink">
              <% @user_data[:links].each do |link| %>
                <li class="profileLink__Item">
                  <i class="icon-link" role="presentation"></i>
                  <a href="<%= add_uri_scheme_name(link[:url]) %>"><%= link[:title] %></a>
                </li>
              <% end %>
            </ul>
          <% else %>
            <p><%= t  :no_links, "%{name} hasn't added any links", :name => @user_data[:short_name] %></p>
          <% end %>
        </div>

        <% if @user_data[:can_edit] %>
          <input type="hidden" name="delete_links" value="1">
          <div class="show-if-editing">
            <table id="edit_links_table">
              <thead>
                <th scope="col"><%= t :links_title, 'Title' %></th>
                <th></th>
                <th scope="col"><%= t :links_url, 'URL' %></th>
              </thead>
              <tbody id="profile_link_fields"></tbody>
            </table>
            <button data-event="addLinkField" class="btn btn-small"><%= t :add_link_field, "Add another link" %></button>
          </div>
        <% end %>
      </div>

      <% if @user_data[:can_edit] %>
        <div class="form-actions no-margin-bottom show-if-editing">
          <button data-event="cancelEditProfile" class="btn">{{#t}}Cancel{{/t}}</button>
          <button class="btn btn-primary">{{#t}}Save Profile{{/t}}</button>
        </div>
      <% end %>
    </div>
  </div>
<% end %>
